package com.yjt.admin.system.controller.org;


import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.yjt.admin.system.constant.SysCons;
import com.yjt.admin.system.entity.Org;
import com.yjt.admin.system.form.OrgForm;
import com.yjt.admin.system.form.org.GroupOrgForm;
import com.yjt.admin.system.service.OrgService;
import com.yjt.admin.system.vo.org.GroupOrgVO;
import com.yjt.admin.system.vo.org.OrgVO;
import com.yjt.common.enums.ModulEnum;
import com.yjt.common.enums.OperTypeEnum;
import com.yjt.common.log.annotation.OperLog;
import com.yjt.common.vo.CommonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 组织表 前端控制器
 * </p>
 *
 * @author zzp
 * @since 2021-10-09
 */
@RefreshScope // 支持Nacos的动态刷新功能
@RestController
@RequestMapping("/admin/system/group-org")
@Api(tags = "集团管理")
public class GroupOrgController {

    @Autowired
    private OrgService orgService;

    @PostMapping("/add")
    @ApiOperation(value = "增加集团信息", notes = "返回JSONObject")
    @OperLog(operModul = ModulEnum.ORG_MODUL, operType = OperTypeEnum.ADD, operDesc = "新增集团信息")
    public CommonResult addGroupOrg(@RequestBody @Validated GroupOrgForm groupOrgForm) {
        OrgForm orgForm = new OrgForm();
        BeanUtils.copyProperties(groupOrgForm, orgForm);
        orgForm.setOrgType(SysCons.ORG_TYPE[1]);
        orgService.addOrg(orgForm);
        return CommonResult.success("集团新增成功");
    }


    @PutMapping("/update")
    @ApiOperation(value = "修改集团信息", notes = "返回JSONObject")
    @OperLog(operModul = ModulEnum.ORG_MODUL, operType = OperTypeEnum.MOD, operDesc = "修改集团信息")
    public CommonResult updateGroupOrg(@RequestBody @Validated GroupOrgForm groupOrgForm) {
        OrgForm orgForm = new OrgForm();
        BeanUtils.copyProperties(groupOrgForm, orgForm);
        orgForm.setOrgType(SysCons.ORG_TYPE[1]);
        orgService.updateOrg(orgForm);
        return CommonResult.success("集团修改成功");
    }


    @GetMapping("/{orgId}")
    @ApiOperation(value = "查询集团信息", notes = "返回JSONObject")
    @ApiImplicitParam(name = "orgId", value = "集团id")
    public CommonResult<GroupOrgVO> getOneGroupOrg(@PathVariable String orgId) {
        OrgVO orgVO = orgService.getOneOrg(orgId);
        GroupOrgVO groupOrgVO = new GroupOrgVO();
        BeanUtils.copyProperties(orgVO, groupOrgVO);
        return CommonResult.success(groupOrgVO, "集团信息查询成功");
    }

    @PutMapping("/forbid")
    @ApiOperation(value = "禁用集团", notes = "返回JSONObject")
    @OperLog(operModul = ModulEnum.ORG_MODUL, operType = OperTypeEnum.MOD, operDesc = "禁用集团")
    @ApiImplicitParam(name = "orgId", value = "集团id", dataType = "string")
    public CommonResult stopGroupOrg(String orgId) {
        //构造参数
        LambdaUpdateWrapper<Org> lambdaUpdateWrapper = Wrappers.<Org>lambdaUpdate()
                .set(Org::getIsForbid, 0)
                .eq(Org::getOrgId, orgId);
        orgService.update(lambdaUpdateWrapper);
        return CommonResult.success("集团禁用成功");
    }


}
